package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Dimension;

import javax.swing.*;

import model.CategoryModel;

import sql.SqlConnector;

import javax.help.*;

import java.net.URL;

import controller.AppointmentController;
import controller.AppointmentSearchController;
import controller.CategoryController;

/**
 * 12-01-2010
 * 
 * @author Dave Nieuwenhuijzen & Martijn Scholten & Michel Rondhuis & Erwin Cavas
 * 
 * Main window of Gidai.
 */
public class MainView {
	private JMenuBar menuBar;
	private JMenu menu;
	private JMenuItem menuItem;
	private JMenuItem menuCat;
	private JMenuItem menuClose;
	private JMenuItem menuHelp;
	private JMenu extra;

	private JMenu viewSwitcher;

	private JTextField searchField;

	private JButton searchBtn;

	private model.CalendarModel calendarModel;
	private model.AppointmentModel appointmentModel;
	private AppointmentController appointmentController;

	private CategoryController categoryController;
	private CategoryModel categoryModel;
	private SqlConnector sqlConnector;

	URL urlIcon = null;
	//URL urlIconSearch = null;

	private AppointmentSearchController appointmentSearchController;

	/**
	 * initializes different classes, builds the mainframe and builds the menubar
	 */
	public MainView() {

		initialiseJComponents();

		sqlConnector = new SqlConnector();
		calendarModel = new model.CalendarModel();
		appointmentModel = new model.AppointmentModel(sqlConnector);
		appointmentController = new AppointmentController(appointmentModel);

		categoryModel = new CategoryModel(sqlConnector);
		categoryController = new CategoryController(categoryModel);

		CalendarView calendarView = new CalendarView(calendarModel, appointmentModel, appointmentController);

		appointmentSearchController = new AppointmentSearchController(appointmentController, searchField);
		appointmentSearchController.addModelForObserving(appointmentModel);

		buildMenu();

		try {

			urlIcon = getClass().getClassLoader().getResource("pics/icon.jpg");
			//urlIconSearch = getClass().getClassLoader().getResource("pics/searchicon.png");

		} catch(NullPointerException x) {
			x.printStackTrace();
			throw x;
		}
		JFrame mainView = new JFrame();
		mainView.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainView.setContentPane(calendarView);
		mainView.setBounds(100, 100, 800, 600);
		mainView.setMinimumSize(new Dimension(800, 600));
		mainView.setTitle("Gidai");
		mainView.setJMenuBar(menuBar);
		mainView.setFocusable(true);
		mainView.requestFocus();
		mainView.setVisible(true);
		mainView.setIconImage(new ImageIcon(urlIcon).getImage());
	}

	/**
	 * The entry point of the application
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		new MainView();
	}

	/**
	 * Initialising the JComponents
	 */
	private void initialiseJComponents() {
		ImageIcon icon = null;
		URL urlIconSearch = null;
		urlIconSearch = getClass().getClassLoader().getResource("pics/searchicon.png");
		menuBar = new JMenuBar();
		viewSwitcher = new JMenu("View");
		menu = new JMenu("Bestand");
		searchField = new JTextField();

		try {
			icon = new ImageIcon(urlIconSearch);
		} catch(NullPointerException x) {

		}

		searchBtn = new JButton(icon);
		extra = new JMenu("Extra");
	}

	/**
	 * Makes the menu which is used in the MainView
	 */
	public void buildMenu() {
		//Initialize components		

		menu.getAccessibleContext().setAccessibleDescription("Testietest");

		viewSwitcher.getAccessibleContext().setAccessibleDescription("View switch tussen week en maand");

		searchField.setMaximumSize(new Dimension(15000,20));
		searchField.addActionListener(appointmentSearchController);
		searchBtn.addActionListener(appointmentSearchController);

		//Add initialized components to the menubar
		menuBar.add(menu);
		menuBar.add(viewSwitcher);
		menuBar.add(extra);
		menuBar.add(Box.createHorizontalGlue());		
		menuBar.add(searchField);
		menuBar.add(searchBtn);

		//List of JMenuItems
		menuItem = new JMenuItem("Nieuwe Afspraak");
		menuItem.getAccessibleContext().setAccessibleDescription("Nieuwe afspraak");
		menuItem.addActionListener(new NewAppointmentListener());

		menuCat = new JMenuItem("Categorie Beheer");
		menuCat.getAccessibleContext().setAccessibleDescription("Categoriebeheer");
		menuCat.addActionListener(new NewCategoryListener());

		JMenuItem menuAbout = new JMenuItem("About");
		menuAbout.addActionListener(new NewAboutListener());

		menuHelp = new JMenuItem("Help");
		menuHelp.addActionListener(new HelpListener());

		menuClose = new JMenuItem("Sluiten");
		menuClose.getAccessibleContext().setAccessibleDescription("Sluiten van Gidai");
		menuClose.addActionListener(new MenuCloseListener());
		//menuClose.addActionListener(new ActionListener());

		JMenuItem menuWeek = new JMenuItem("Week");
		menuWeek.getAccessibleContext().setAccessibleDescription("Het wisselen naar week");
		menuWeek.addActionListener(new SwitchWeekListener());

		JMenuItem menuMaand = new JMenuItem("Maand");
		menuMaand.addActionListener(new SwitchMonthListener());

		JMenuItem menuNext = new JMenuItem("Next");
		menuNext.addActionListener(new NextListener());

		JMenuItem menuPrevious = new JMenuItem("Previous");
		menuPrevious.addActionListener(new PreviousListener());

		menu.add(menuItem);
		menu.add(menuCat);
		menu.addSeparator();
		menu.add(menuClose);

		viewSwitcher.add(menuWeek);
		viewSwitcher.add(menuMaand);
		viewSwitcher.addSeparator();
		viewSwitcher.add(menuNext);
		viewSwitcher.add(menuPrevious);
		extra.add(menuHelp);
		extra.add(menuAbout);
	}

	/**
	 * This ActionListener is the controller for exiting the Gidai program wich is used in the menu.
	 * 
	 * @author Martijn Scholten
	 */
	class MenuCloseListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	}

	/**
	 * This ActionListener is used for switching the calenderModel to a week view.
	 * 
	 * @author Martijn Scholten
	 */
	class SwitchWeekListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			calendarModel.setCalendarViewMode(model.CalendarViewMode.week);
		}
	}

	/**
	 * This ActionListener is used for switching the calenderModel to a month view.
	 * 
	 * @author Martijn Scholten
	 */
	class SwitchMonthListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			calendarModel.setCalendarViewMode(model.CalendarViewMode.month);
		}
	}

	/**
	 * This ActionListener is used for going to the calenderModel.next().
	 * 
	 * @author Dave Nieuwenhuijzen
	 */
	class NextListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			calendarModel.next();
		}
	}
	/**
	 * This ActionListener is used for going to the calenderModel.next()
	 * 
	 * @author Dave Nieuwenhuijzen
	 */
	class PreviousListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			calendarModel.previous();
		}
	}

	/**
	 * This ActionListener will show the AppointmentView for adding a new Appointment.
	 * 
	 * @author Martijn Scholten
	 */
	class NewAppointmentListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			appointmentController.newAppointment();
		}
	}

	/**
	 * This ActionListener shows a new AboutView
	 * 
	 * @author Michel Rondhuis
	 */
	class NewAboutListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			new AboutView();
		}
	}

	/**
	 * This ActionListener will call for a new Category
	 * 
	 * @author Martijn Scholten
	 */
	class NewCategoryListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			categoryController.newCategory();
		}
	}

	/**
	 * This ActionListener will call the help
	 * @author Martijn Scholten, Maarten Ezendam
	 */
	class HelpListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JHelp helpViewer = null;

			try {
				ClassLoader cl = MainView.class.getClassLoader();
				URL url = HelpSet.findHelpSet(cl, "main.hs");
				helpViewer = new JHelp(new HelpSet(cl, url));
			} catch (Exception x) {

			}

			JFrame frame = new JFrame();
			frame.setSize(500,500);
			frame.getContentPane().add(helpViewer);
			frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			frame.setVisible(true);
		}
	}
}